package com.flashflexpro.tanksinbattle.battlescene;

import com.flashflexpro.tanksinbattle.battlescene.dto.BattleSceneDto;
import com.flashflexpro.tanksinbattle.battlescene.dto.TurretVO;
import com.flashflexpro.tanksinbattle.battlescene.dto.XyVO;
import com.flashflexpro.tanksinbattle.battlescenedoor.dto.DoorDto;
import com.flashflexpro.tanksinbattle.battlescenegunfort.dto.GunFortDto;
import com.flashflexpro.tanksinbattle.battlescenetank.dto.TankDto;
import com.flashflexpro.tanksinbattle.scene.dto.DependencyDto;
import com.flashflexpro.tanksinbattle.scene.dto.FutureModuleInstDto;
import com.flashflexpro.tanksinbattle.scene.dto.ModuleDto;
import com.flashflexpro.tanksinbattle.scene.dto.UrlDependencyDto;
import com.flashflexpro.tanksinbattle.utils.DependencyUtil;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author gyang@brightcove.com
 */
public class BattleSceneFacade {

    public FutureModuleInstDto getBattleScene() throws IOException {
        BattleSceneDto dto = new BattleSceneDto();
        dto.setDependency( new UrlDependencyDto( "scene-battle.swf", "scene-battle-skin.swf",
                "com.flashflexpro.tanksinbattle.battlescene.BattleScene" ) );

        DoorDto[] doors = new DoorDto[1];
        doors[ 0 ] = new DoorDto();
        doors[ 0 ].setDependency( new UrlDependencyDto( "scene-battle-door.swf", "scene-battle-door-skin.swf",
                "com.flashflexpro.tanksinbattle.battlescenedoor.BattleSceneDoor") );
        doors[ 0 ].setInitialPosition( new XyVO( 90, 80 ));
        dto.setDoors( doors );
        
        GunFortDto[] forts = new GunFortDto[1];
        forts[ 0 ] = new GunFortDto();
        forts[ 0 ].setDependency( new UrlDependencyDto( "scene-battle-gunfort.swf", "scene-battle-gunfort-skin.swf",
                "com.flashflexpro.tanksinbattle.battlescenegunfort.BattleSceneGunfort"));
        forts[ 0 ].setInitialPosition( new XyVO( 240, 220 ));
        TurretVO gtr = new TurretVO();
        gtr.setBulletPower( 10 );
        gtr.setBulletVelocity( 8 );
        gtr.setFireRate( 2 );
        gtr.setRotateVelocity( 0.1F );
        forts[ 0 ].setTurret( gtr );

        dto.setForts( forts );

        TankDto[] tanks = new TankDto[ 1 ];
        tanks[ 0 ] = new TankDto();
        tanks[ 0 ].setDependency( new UrlDependencyDto( "scene-battle-tank.swf", "scene-battle-tank-skin.swf",
                "com.flashflexpro.tanksinbattle.battlescenetank.Tank" ));
        tanks[ 0 ].setInitialPosition( new XyVO( 390, 110));
        tanks[ 0 ].setMaxVelocity( 10 );
        TurretVO tr = new TurretVO();
        tr.setBulletPower( 10 );
        tr.setBulletVelocity( 18 );
        tr.setFireRate( 4 );
        tr.setRotateVelocity( 0.3F );
        tanks[ 0 ].setTurret( tr );
        dto.setTanks( tanks );

        FutureModuleInstDto rt = new FutureModuleInstDto();
        rt.setDependencies( getBitFromBattleDto( dto ) );
        rt.setDto( DependencyUtil.getAmfBytesFromObject( dto ) );
        rt.setMain( dto.getDependency().getEntryClass() );
        return rt;
    }

    private DependencyDto[] getBitFromBattleDto( BattleSceneDto dto ){
        ArrayList<DependencyDto> al = new ArrayList<DependencyDto>();
        addModules( al, dto.getDoors() );
        addModules( al, dto.getForts() );
        addModules( al, dto.getTanks() );
        al.add( dto.getDependency() );
        DependencyDto[] rt = new DependencyDto[al.size()];
        return al.toArray( rt );
    }

    private void addModules(ArrayList<DependencyDto> al, ModuleDto[] doors){
        if( doors == null ){
            return;
        }
        for ( ModuleDto doorDto : doors ){
            if( doorDto == null ){
                System.out.println( "Ignoring null ModuleDto!" );
                continue;
            }
            if( !al.contains( doorDto)){
                al.add( doorDto.getDependency() );
            }
        }
    }

}
